﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMQLNhaHang.CustomControl;
using QLNHBUS;
using QLNHDAO;

namespace PMQLNhaHang
{
    public partial class fmQLKVBAn : Form
    {
        private List<exPictureBox> _dsBanAnPT;
        private int _indexBanAn = -1;

        public delegate void GuiThongTinKhuVucHandler(int khuvuc);
        public delegate void ReloadDanhSachBanAn();
        public ReloadDanhSachBanAn ReloadBA;

        public delegate void ReloadDanhSachKhuVuc();
        public ReloadDanhSachKhuVuc ReloadKV;
        public fmQLKVBAn()
        {
            InitializeComponent();
            _dsBanAnPT = new List<exPictureBox>();
            ReloadBA = LoadBanAn;
            ReloadKV = LoadDSKVuc;
        }

        private void btnThemKhuVuc_Click(object sender, EventArgs e)
        {
            fmQLKVBAnKhuVuc kv = new fmQLKVBAnKhuVuc();
            kv.formParent = this;
            kv.ShowDialog();
        }
        private void btnXoaKhuVuc_Click(object sender, EventArgs e)
        {
            var sel = (exListBoxItem)exlbKhuVuc.SelectedItem;
            KhuVuc kv = KhuVucBUS.LayKhuVucTheoMaKhuVuc(sel.Id);
            kv.Xoa = true;
            KhuVucBUS.XoaCapNhatDuLieu(kv);
            LoadDSKVuc();
        }
        private void btnSuaKhuVuc_Click(object sender, EventArgs e)
        {
            var sel = (exListBoxItem)exlbKhuVuc.SelectedItem;
            fmQLKVBAnKhuVuc qlkv = new fmQLKVBAnKhuVuc();
            GuiThongTinKhuVucHandler gttkv = qlkv.NhanMaKhuVuc;
            qlkv.formParent = this;
            gttkv(sel.Id);
            qlkv.ShowDialog();
        }
        private void btnThemBanAn_Click(object sender, EventArgs e)
        {

            fmQLKVBAnBanAn ba = new fmQLKVBAnBanAn();
            GuiThongTinKhuVucHandler gttkv = ba.NhanMaKhuVuc;
            var sel = (exListBoxItem)exlbKhuVuc.SelectedItem;
            gttkv(sel.Id);
            ba.formParent = this;
            ba.ShowDialog();
        }
        private void fmQLKVBAn_Load(object sender, EventArgs e)
        {
            LoadDSKVuc();
            exlbKhuVuc.SelectedIndex = 0;
        }
        private void exlbKhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadBanAn();
        }
        private void btnXoaBanAn_Click(object sender, EventArgs e)
        {
            int maBanAn = int.Parse(_dsBanAnPT[_indexBanAn].Name);
            BanAn ba = BanAnBUS.LayBanAnTheoMaBanAn(maBanAn);
            ba.Xoa = true;
            BanAnBUS.XoaCapNhapBanAn(ba);
            LoadBanAn();
        }

        #region Phương thức
        private void TheHienDanhSachBanAn(List<BanAn> dsBanAn)
        {
            flowLayoutPanel1.Controls.Clear();
            _dsBanAnPT.Clear();
            exPictureBox tempPic;
            int xlocation, ylocation;
            int exptHeigh = 100, exptWidth = 85;

            for (int i = 0; i < dsBanAn.Count; i++)
            {
                tempPic = new exPictureBox();
                tempPic.IsActive = false;
                xlocation = exptWidth + 40;
                ylocation = exptHeigh + 40;
                tempPic.Location = new Point(xlocation * (i % 3), ylocation * (i / 3));
                tempPic.Location = new Point(xlocation, ylocation);
                tempPic.Name = dsBanAn[i].MaBanAn.ToString();
                tempPic.NameTable = dsBanAn[i].Ten;
                tempPic.IdTable = i;
                tempPic.Size = new Size(exptWidth, exptHeigh);
                tempPic.SizeMode = PictureBoxSizeMode.Zoom;
                tempPic.TabIndex = 0;
                tempPic.TabStop = false;
                tempPic.Click += new EventHandler(tempPic_Click);
                flowLayoutPanel1.Controls.Add(tempPic);
                _dsBanAnPT.Add(tempPic);
            }
        }
        private void tempPic_Click(object sender, EventArgs e)
         {
             var ptb = (exPictureBox)sender;
             _indexBanAn = ptb.IdTable;
             fmPhucVu.KhoiPhucTrangTHaiDanhSachBanAn(_dsBanAnPT);
             fmPhucVu.PBChangeColorClick(ptb);

         }
        public void LoadBanAn()
        {
            var sel = (exListBoxItem)exlbKhuVuc.SelectedItem;
            var dsBanAn = BanAnBUS.LayDanhSachBanAn(sel.Id);
            _indexBanAn = -1;
            TheHienDanhSachBanAn(dsBanAn);
        }
        public void LoadDSKVuc()
        {
            var dsKhuVuc = KhuVucBUS.LayDanhSachKhuVuc();
            fmPhucVu.TheHienDanhSachKhuVu(dsKhuVuc, exlbKhuVuc);
        }
        #endregion




    }
}
